export interface paramsType {
  pageNumber: number;
  pageSize: number;
}

//分页hooks
export default function useTurn<T>(pageData: any, defaultSize?: number) {
  const params = reactive({
    //分页数据，用于请求
    page: 1,
    size: defaultSize ? defaultSize : 10,
  });
  const pageList = ref<T[]>([]);
  const pageLoading = ref(false);
  const paginationTotal = ref(0);
  const paginationCurrent = ref(0);

  //获取分页数据
  async function loadData() {
    pageLoading.value = true;
    try {
      const res = await pageData(params);
      if (!res) return (pageLoading.value = false);
      const { content, pageable } = res;
      pageList.value = content || [];
      paginationCurrent.value = pageable.pageNumber;
      paginationTotal.value = pageable.totalItems;
    } catch (err) {
      console.log(err);
    }
    pageLoading.value = false;
    return Promise.resolve();
  }

  const pageChange = (val: number, key: 'page' | 'size') => {
    params[key] = val;
    loadData();
  };
  loadData();
  return {
    loadData, //刷新数据
    pageChange, //page改变
    pageList, //表格数据
    paginationCurrent, //当前页数
    paginationTotal, //总数
    pageLoading, //加载Loading
  };
}
